package com.briup.jz.web.controller;

import com.briup.jz.bean.Category;
import com.briup.jz.service.ICategoryService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 潘京博
 * @project: jz
 * @description: 栏目控制器
 * @create: 2020/6/13 11:17
 */
@RestController
@RequestMapping("/category")
@Api(description = "栏目管理接口")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    @ApiOperation("添加或更新栏目，不传入id则为添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "不传入id则为添加", required = false, paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "nu", value = "序号", required = false, paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父栏目的id", required = false, paramType = "query")
    })
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Category category){
        categoryService.saveOrUpdate(category);
        return MessageUtil.success("操作成功");
    }

    @ApiOperation(value = "通过id删除栏目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "唯一编号", required = true, paramType = "query")
    })
    @GetMapping("deleteById")
    public Message deleteById(long id){
        categoryService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation("查找全部栏目及其子栏目")
    @GetMapping("findAllWithChildren")
    public Message findAllWithChildren(){
        return MessageUtil.success("查找成功", categoryService.findAllWithChildren());
    }

    @GetMapping("findAll")
    @ApiOperation("查找所有栏目")
    public Message findAll(){
        return MessageUtil.success("查找成功", categoryService.findAll());
    }

    @GetMapping("findById")
    @ApiOperation("通过id查找栏目")
    public Message findById(long id){
        return MessageUtil.success("查找成功", categoryService.findById(id));
    }

}
